<?php

namespace app\db\controller;
use app\db\library\Auth;
use think\Cookie;
use think\Hook;
use think\Validate;

class Index extends Common
{
    protected $noNeedLogin = ['index','login'];
    protected $noNeedRight = ['index','login','logout'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
        $auth = $this->auth;
        //监听登录事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        //监听注册事件
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        //监听删除事件
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        //监听退出事件
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
    }

    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 用户登录
     *
     * @param string $account 账号
     * @param string $password 密码
     */
    public function login()
    {
        if ($this->auth->isLogin()) {
            $this->success(__("You've logged in, do not login again"));
        }
        if ($this->request->isPost()) {
            $account  = $this->request->request('account');
            $password = $this->request->request('password');
            // $keeplogin = (int)$this->request->post('keeplogin');  //是否保持登录
            $rule = [       //验证规则
                'account'   => 'require',
                'password'  => 'require',
            ];
            $msg = [   //验证提示信息
                'account.require'  => 'Account can not be empty',
                'password.require' => 'Password can not be empty',
                // 'account.regex'   => 'The password is a combination of 4-20 letters or numbers',
                // 'password.regex'  => 'The password is a combination of 6-20 letters or numbers',
            ];
            $data = [  //验证数据
                'account'   => $account,
                'password'  => $password,
            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);//数据验证
            if ($result) {
                $ret = $this->auth->login($account, $password);
                if ($ret) {
                    #获取系统基础模板
                    $basicTemplateId = @model('DbTemplate')->getSysBasicTemplateId()[0]['id'];
                    $data = ['userinfo' => $this->auth->getUsercareinfo('id,account,name,heading_img,default_database_show_id',true)];
                    $data['userinfo']['basicTemplateId'] = $basicTemplateId;
                    $this->success(__('Logged in successful'), $data);
                } else {
                    $this->error($this->auth->getError());
                }
            }else{
                $this->error(__($validate->getError()));
            }
        }else{
            $this->error(__('No Post request'));
        }

    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }




}
